package com.prince.util;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import com.prince.exception.EncException;


/**
 * 加密工具类
 * 
 * md5加密出来的长度是32位
 * 
 * sha加密出来的长度是40位
 * 
 * @author zhangdaihao
 * 
 */
public class Encrypt {

	/**
	 * 测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
//		// md5加密测试
//		String md5_1 = md5("123456");
//		String md5_2 = md5("zhangdaihao");
//		//System.out.println(md5_1 + "\n" + md5_2);
//		// sha加密测试
//		String sha_1 = sha("123456");
//		String sha_2 = sha("zhangdaihao");
//		//System.out.println(sha_1 + "\n" + sha_2);
		byte[] bytes = md5("123123").getBytes();
		String encode = Base64.encode(bytes);
		System.out.println(encode);
	}

	/**
	 * 加密
	 * 
	 * @param inputText
	 */
	public static String e(String inputText) {
		return md5(inputText);
	}

	/**
	 * 二次加密，应该破解不了了吧？
	 * 
	 * @param inputText
	 * @return
	 */
	public static String md5AndSha(String inputText) {
		return sha(md5(inputText));
	}

	/**
	 * md5加密
	 * 
	 * @param inputText
	 * @return
	 */
	public static String md5(String inputText) {
		return encrypt(inputText, "md5");
	}

	/**
	 * sha加密
	 * 
	 * @param inputText
	 * @return
	 */
	public static String sha(String inputText) {
		return encrypt(inputText, "sha-1");
	}

	/**
	 * md5或者sha-1加密
	 * 
	 * @param inputText
	 *            要加密的内容
	 * @param algorithmName
	 *            加密算法名称：md5或者sha-1，不区分大小写
	 * @return
	 */
	private static String encrypt(String inputText, String algorithmName) {
		if (inputText == null || "".equals(inputText.trim())) {
			throw new IllegalArgumentException("请输入要加密的内容");
		}
		if (algorithmName == null || "".equals(algorithmName.trim())) {
			algorithmName = "md5";
		}
		String encryptText = null;
		try {
			MessageDigest m = MessageDigest.getInstance(algorithmName);
			m.update(inputText.getBytes("UTF8"));
			byte s[] = m.digest();
			// m.digest(inputText.getBytes("UTF8"));
			return hex(s);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encryptText;
	}

	/**
	 * 返回十六进制字符串
	 * 
	 * @param arr
	 * @return
	 */
	private static String hex(byte[] arr) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; ++i) {
			sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
		}
		return sb.toString();
	}

	/**
	 * 3D保护ES
	 *
	 * @param plainText 普通文本
	 * @return
	 * @throws Exception
	 */
	public static String protectMode(String secretKey, String plainText)
	        throws EncException {
		String iv = "01234567";
		String encoding = "utf-8";
		String Algorithm = "desede/CBC/PKCS5Padding"; //定义 保护方法,可用 DES,DESede,Blowfish
	    Key deskey = null;
	    try {
	        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
	        SecretKeyFactory keyfactory = SecretKeyFactory
	                .getInstance("desede");
	        deskey = keyfactory.generateSecret(spec);
	        Cipher cipher = Cipher.getInstance(Algorithm);
	        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
	        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
	        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
	        return encryptBASE64(encryptData);
	    } catch (Exception e) {
	        throw new EncException(1, "ProtectUtils", "protectMode", "保护错误", null);
	    }
	}
	
	/**
     * 解密BASE64
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) throws Exception {
        return Base64.decode(key);
    }

    /**
     * 加密BASE64
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] key) throws Exception {
        return Base64.encode(key);
    }
}
